import sys, getopt, random, requests, importlib, os, time
from urllib import parse


def main(args):
    try:
        options, args = getopt.getopt(args, "hc:u:r:n:v", ['help', 'cookie=', 'url=', 'rule=', 'name=','template='])
    except getopt.GetoptError:
        sys.exit(1)
    base_path = os.path.abspath('./')
    url = ''
    cookie = ''
    rule = 'rules.default'
    name = time.time()
    view_path = ''

    for option, value in options:
        if option in ("-h", "--help"):
            print('python fiction.py --cookie=/home/cookie.txt --url=http://www.luoqiu.com/read/3713/index.html --rule=rules.luoqiu --name=yasao --template=templates/view.html')
            sys.exit(0)
        if option in ('-u', '--url'):
            url = value
        if option in ('-c', '--cookie'):
            cookie = value
        if option in ('-r', '--rule'):
            rule = value
        if option in ('-n', '--name'):
            name = value
        if option in ('-v','--template'):
            view_path = value

    headers = {
        'User-Agent': r'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36',
        'Referer': r'https://www.baidu.com/s?wd=%E5%B0%8F%E8%AF%B4&rsv_spt=1',
        'Accept': r'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Connection': 'keep-alive',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7',
        'Accept-Encoding': 'gzip, deflate',
        'Cache-Control' : 'no-cache',
        'Pragma' : 'no-cache'
    }
    user_agent_list = [
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1",
        "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6",
        "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6",
        "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1",
        "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5",
        "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
        "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
        "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
        "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
        "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
        "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.0 Safari/536.3",
        "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24",
        "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24"
    ]
    ua = random.choice(user_agent_list)  # 从user_agent_list中随机抽取出一个字符串

    headers['User-Agent'] = ua
    cookies = {}
    if cookie != '' and os.path.exists(cookie):
        cookies = {}
        with open(cookie, 'r') as f:
            content = f.read()

            if content is not None and content != '':
                for item in content.split(';'):
                    s_pos = item.find('=')
                    if s_pos is not None:
                        cookies[item[0:s_pos].strip()] = item[s_pos + 1:]

    s = requests.Session()
    s.get(url)
    req = s.get(url, headers=headers, cookies=cookies)

    if req.status_code != 200:
        print('获取远程URL出错')
        sys.exit(1)

    res = req.text.encode(req.encoding)

    fiction_rule = importlib.import_module(rule)
    fiction_rule = fiction_rule.Rule(url)
    # print(res)
    fiction_catalog_list = fiction_rule.resolve_catalog(res)
    # print(fiction_catalog_list)
    if fiction_catalog_list is None or len(fiction_catalog_list) <= 0:
        print('解析小说目录失败')
        sys.exit(1)
    else:
        print('解析小说目录完成,共 ' + str(len(fiction_catalog_list)) + ' 个章节')
    fiction_content_path = os.path.join(base_path, 'chapter', name)
    if not os.path.exists(fiction_content_path):
        os.makedirs(fiction_content_path)

    # print(fiction_catalog_list)
    bar_length = 20
    percent = 0
    mod = len(fiction_catalog_list) / 100

    # 遍历小说目录，获取小说内容并保存
    for catalog in fiction_catalog_list:
        req = s.get(catalog['url'], headers=headers, cookies=cookies)

        if req.status_code != 200:
            print('抓取 ' + catalog['url'] + ' 失败,http code:' + str(req.status_code))
        res = req.text.encode(req.encoding)

        fiction_content = fiction_rule.resolve_content(res)
        print('正在处理：' + catalog['text'])

        #print(fiction_content)
        fiction_content_chapter_path = os.path.join(fiction_content_path, catalog['code'])
        if os.path.exists(view_path):
            fiction_content_chapter_path = fiction_content_chapter_path + '.html'
            template(view_path,fiction_content_chapter_path,catalog['text'], fiction_content)
        else:
            fiction_content = catalog['text'] + "\r\n" + fiction_content
            fiction_content_chapter_path = fiction_content_chapter_path + '.txt'
            with open(fiction_content_chapter_path, 'w', encoding='utf-8') as f:
                f.write(fiction_content)

        percent = percent + 1
        if percent % mod == 0 :

            hashes = '#' * int(percent / 100.0 * bar_length)
            spaces = ' ' * (bar_length - len(hashes))
            sys.stdout.write("\rPercent: [%s] %d%%" % (hashes + spaces, percent/mod))
            sys.stdout.flush()

    print("\r\n")
    print('处理完成!')
    sys.exit(0)


def template(view_path,file_path ,title,content):
    if content is not None:
        content = '<p>' + content.replace('\r','\n').replace('\n','</p><p>') + '</p>'
        with open(view_path,'r', encoding='utf-8') as f:
            tpl = f.read()
            content = tpl.replace('{{title}}',title).replace('{{content}}',content)

        with open(file_path, 'w', encoding='utf-8') as f:
            f.write(content)


if __name__ == '__main__':
    main(sys.argv[1:])
